#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define DEFAULT_HOST "127.0.0.1"
#define DEFAULT_PORT "27015"

void p(const char *sr, int n)
{
    char buf[100];
    n = wsprintfA(buf, "Bytes %s: %d\n", sr, n);
    WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), buf, n, NULL, NULL);
}

void mainCRTStartup()
{
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2,2), &wsaData);
    }

    struct addrinfo hints =
    {
        0,
        AF_UNSPEC,
        SOCK_DGRAM,
        IPPROTO_UDP,
        0,
        NULL,
        NULL,
        NULL,
    };

    struct addrinfo *result = NULL;
    getaddrinfo(DEFAULT_HOST, DEFAULT_PORT, &hints, &result);
    SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

    #define sendbuf "this is a test"
    p("sent", sendto(sock, sendbuf, sizeof sendbuf - 1, 0, result->ai_addr, (int)result->ai_addrlen));

    freeaddrinfo(result);

    {
        char buf[32];
        struct sockaddr addr;
        int len = sizeof(addr);
        p("received", recvfrom(sock, buf, sizeof buf, 0, &addr, &len));
    }

    closesocket(sock);
    WSACleanup();

    ExitProcess(0);
}